-- 金融帝国网游与手游，QQ群2199361
--本文档是用maps与loveframes的整合，失败，整合摇杆
require "tiledmap"

require "libraries.third-party.class" --摇杆需要引入的

gKeyPressed = {}
gCamX,gCamY = 100,100

function loadMap(path)
  TiledMap_Load(path)
  gCamX,gCamY = gMapWidth * gTileWidth / 2, gMapHeight * gTileHeight / 2
end

function love.load()
	loadMap("map/map01.tmx")
  
    --绘制摇杆
  bg = love.graphics.newImage("resources/images/layout.png")
	center = love.graphics.newImage("resources/images/center.png")
	hero = love.graphics.newImage("resources/images/aircraft-ne.png")	
	gr=love.graphics
	point = {x=200,y=200}	
	joy = require "libraries/third-party/_joystick"(bg,center)
	joy:setPoint(130,love.graphics.getHeight()-130)	
end

function love.keyreleased( key )
	gKeyPressed[key] = nil
end

function love.keypressed( key, unicode ) 
	gKeyPressed[key] = true 
	if (key == "escape") then os.exit(0) end
	if (key == " ") then -- space = next mal
		gMapNum = (gMapNum or 1) + 1
		if (gMapNum > 3) then gMapNum = 1 end
		loadMap(string.format("map/map%02d.tmx",gMapNum))
	end
end

function love.update( dt )
	local s = 500*dt
	if (gKeyPressed.up) then gCamY = gCamY - s end
	if (gKeyPressed.down) then gCamY = gCamY + s end
	if (gKeyPressed.left) then gCamX = gCamX - s end
	if (gKeyPressed.right) then gCamX = gCamX + s end
  -- Keep camera within map bounds
  gCamX = math.min(math.max(gCamX, 0), gMapWidth * gTileWidth)
  gCamY = math.min(math.max(gCamY, 0), gMapHeight * gTileHeight)
  
     --摇杆更新
  if joy.dir == 1 then 		
	elseif joy.dir == 2 then 
		point.y = point.y + 300*dt * joy.power
		gCamY = gCamY + 300*dt * joy.power
	elseif joy.dir == 3 then 
	elseif joy.dir == 4 then 
		point.x = point.x - 300*dt
		gCamX = gCamX - 300*dt
	elseif joy.dir == 6 then 
		point.x = point.x + 300*dt
		gCamX = gCamX + 300*dt
	elseif joy.dir == 7 then 
	elseif joy.dir == 8 then 
		point.y = point.y - 300*dt
		gCamY = gCamY - 300*dt
	elseif joy.dir == 9 then 
	end
end

function love.draw()
  love.graphics.print('arrow-keys=scroll, space=next map', 50, 50)
  --此处颜色我做了0~1的修正
	love.graphics.setBackgroundColor(80/255,80/255,80/255)
	TiledMap_DrawNearCam(gCamX,gCamY)
  
  joy:draw()--摇杆
end


--摇杆
if love.system.getOS() == "Windows" then 


	function love.mousepressed(x,y)
		joy:touchpressed(1,x,y)
	end

	function love.mousemoved(x,y)
		joy:touchmoved(1,x,y)

	end

	function love.mousereleased(x,y)
		joy:touchreleased(1,x,y)
		
		
	end



elseif love.system.getOS() == "Android" then 

	function love.touchpressed(...)
		joy:touchpressed(...)
	end

	function love.touchmoved(...)
		joy:touchmoved(...)
	end

	function love.touchreleased(...)
	joy:touchreleased(...)
	end


end